/*
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <config.h>
#include <common.h>
//#include <asm/arch/mach-api.h>
#include "nxp-fb.h"

#include <asm/arch/nexell.h>
#include <asm/arch/nx_gpio.h>

/*
 MCU_SPI1_FRM       GPIOC10   --> 5     LCD_CS     
 MCU_SPI1_RXD       GPIOC11   --> 6     LCD_RXD    

 MCU_SPI1_TXD       GPIOC12   --> 7     LCD_SDA    
 MCU_SPI1_CLK       GPIOC9    --> 8     LCD_SCL    

 GPIOC0             GPIOC0    --> 12    LCD_RST    

 MCU_CAMERA_PD      GPIOC6    --> 13    LCD_SCL    
 MCU_CAM1_HYNC      GPIOE13   --> 18    LCD_RST    
*/

#if 1
#define	VBPD  8//20//25
#define	VFPD  8//50//35
#define	VSPW  2//2//2
   
#define	HBPD  205//180//200//130//120*
#define	HFPD  45//70//60//85//80
#define	HSPW  12//12//5//12
#endif


/*
#define LCD_SDA                 (PAD_GPIO_C + 12)           //7
#define LCD_SCL                 (PAD_GPIO_C + 9 )           //8

#define LCD_RST                 (PAD_GPIO_C + 0 )           //12 
#define LCD_CS                  (PAD_GPIO_C + 10)           //5

#define LCD_RXD                 (PAD_GPIO_C + 11)           //6

#define LCD_PWR                 (PAD_GPIO_C + 24)
*/
#define PAD_GPIO_A      0
#define PAD_GPIO_B      1
#define PAD_GPIO_C      2
#define PAD_GPIO_D      3
#define PAD_GPIO_E      4
#define PAD_GPIO_F      5

#define LCD_SDA                 PAD_GPIO_C , 29             //7    绿-> GPIOC29 (SPI0_CLK)
#define LCD_SCL                 PAD_GPIO_C , 30             //8    蓝-> GPIOC30 (SPI0_CS)

#define LCD_RST                 PAD_GPIO_C ,  9             //12   橙-> GPIOC9
#define LCD_CS                  PAD_GPIO_C , 31             //5    灰-> GPIOC31 (SPI0_MOSI)

#define LCD_RXD                 PAD_GPIO_D ,  0             //6    红-> GPIOD0  (SPI0_MISO)

#define LCD_PWR                 PAD_GPIO_C , 10             //可以不接


//----------------------------------------------
#define		LCD_SDA_CLR		    nx_gpio_set_output_value(LCD_SDA, 0)
#define		LCD_SDA_SET		    nx_gpio_set_output_value(LCD_SDA, 1)
#define		LCD_SDA_GET		    nx_gpio_get_output_value(LCD_SDA)

#define		LCD_SCL_CLR			nx_gpio_set_output_value(LCD_SCL, 0)
#define		LCD_SCL_SET		    nx_gpio_set_output_value(LCD_SCL, 1)
#define		LCD_SCL_GET		    nx_gpio_get_output_value(LCD_SCL)

#define		LCD_RST_CLR			nx_gpio_set_output_value(LCD_RST, 0)
#define		LCD_RST_SET			nx_gpio_set_output_value(LCD_RST, 1)
#define		LCD_RST_GET		    nx_gpio_get_output_value(LCD_RST)

#define		LCD_CS_CLR			nx_gpio_set_output_value(LCD_CS, 0)
#define		LCD_CS_SET			nx_gpio_set_output_value(LCD_CS, 1)
#define		LCD_CS_GET		    nx_gpio_get_output_value(LCD_CS)

#define		LCD_RXD_GET		    nx_gpio_get_output_value(LCD_RXD)

#define     LCD_PWR_CLR         nx_gpio_set_output_value(LCD_PWR, 0)
#define     LCD_PWR_SET         nx_gpio_set_output_value(LCD_PWR, 1)

/*************************************************************/
/********************** 以下代码为通用代码 *******************/
/*************************************************************/
void GPIO_CFG(void);
void LCD_SPI_Init(void);
void spi_delay(void);
void LCD_POWER_OFF();
void LCD_POWER_ON();
void W_C(int data);
void W_D(int data);
void Wr_com_data16(unsigned char c,unsigned short value);
void writed16(int data);
void GP_COMMAD_PA(int a);
void LCD_Init();
unsigned short SPI_READ(void);
int SPI_READ_ID(void);
int ssd2828_init(void);

void delay_ms(unsigned int val)
{
    mdelay(val);
}

void spi_delay(void)
{
  /*
  volatile	int i;
  for(i=0;i<2;i++);
  */
  //mdelay(1);
  //udelay(100);
  //udelay(1);
  udelay(10);
  return;
}

void  LCD_POWER_ON()
{
    //POWER OFF STATE -> SLEEP MODE
    //DCDC_EN L->H
    LCD_PWR_SET;
    mdelay(30);

    LCD_RST_SET;
    mdelay(10);

    //DCDC_EN H->L
    LCD_PWR_CLR;
    mdelay(30);
}

void  LCD_POWER_OFF()
{
    LCD_SDA_CLR;
    LCD_SCL_CLR;
    delay_ms(20);

    LCD_RST_CLR;
    delay_ms(20);

    LCD_CS_CLR;
    delay_ms(20);

    //DCDC_EN H->L
    LCD_PWR_CLR;
    mdelay(50); // Delay 1ms
}

void W_C(int data)
{
    // RS=0
    int i;

    LCD_CS_CLR;  spi_delay();
    LCD_SDA_CLR; spi_delay();
    LCD_SCL_CLR; spi_delay();
    LCD_SCL_SET; spi_delay();

    for(i=0;i<8;i++)
    {
        if (data & 0x80)
        {
            LCD_SDA_SET; spi_delay();
            //LCD_SDA_SET;
        }
        else
        {
            LCD_SDA_CLR; spi_delay();
            //LCD_SDA_CLR;
        }

        data<<= 1;
        LCD_SCL_CLR; spi_delay();
        LCD_SCL_SET; spi_delay();
    }
    LCD_CS_SET;spi_delay();
}

void W_D(int data)
{
    //RS=1
    int i;

    LCD_CS_CLR;  spi_delay();
    LCD_SDA_SET; spi_delay();
    LCD_SCL_CLR; spi_delay();
    LCD_SCL_SET; spi_delay();

    for(i=0;i<8;i++)
    {
        if (data & 0x80)
        {
            LCD_SDA_SET; spi_delay();
            //LCD_SDA_SET;
        }
        else
        {
            LCD_SDA_CLR; spi_delay();
            //LCD_SDA_CLR;
        }

        data<<= 1;
        LCD_SCL_CLR; spi_delay();
        LCD_SCL_SET; spi_delay();
    }
    LCD_CS_SET;spi_delay();
}

void Wr_com_data16(unsigned char c,unsigned short value)
{
	LCD_CS_CLR;
	W_C(c);
	W_D(value&0xff);
	W_D((value>>8)&0xff);	
	LCD_CS_SET;	
}

void writed16(int data)
{
    //RS=1
    int i;

    LCD_CS_CLR; spi_delay();

    LCD_SDA_SET;//BIT8=0 COMM
    spi_delay();

    LCD_SCL_CLR; spi_delay();

    LCD_SCL_SET;spi_delay();

    for(i=0;i<8;i++)
    {

        if (data & 0x80)
        { 
            LCD_SDA_SET;spi_delay();
        }
        else
        {
            LCD_SDA_CLR;spi_delay();
        }

        data<<= 1;

        LCD_SCL_CLR; spi_delay();
        LCD_SCL_SET; spi_delay();
    }
    LCD_CS_SET;spi_delay();
}
 

void GP_COMMAD_PA(int a)
{
 	W_C(0xBC);
	writed16(a);
	writed16(a>>8);
	W_C(0xBF);
}

void GPIO_CFG(void)
{
    //设置引脚功能为输出功能(有的gpio输出功能是设置为1)
	nx_gpio_set_pad_function(LCD_RST, 0);
	nx_gpio_set_pad_function(LCD_SDA, 0);
	nx_gpio_set_pad_function(LCD_SCL, 0);
	nx_gpio_set_pad_function(LCD_CS , 0);
	nx_gpio_set_pad_function(LCD_RXD, 0);

    //使能gpio输出
	nx_gpio_set_output_enable(LCD_RST, 1);
	nx_gpio_set_output_enable(LCD_SDA, 1);
	nx_gpio_set_output_enable(LCD_SCL, 1);
	nx_gpio_set_output_enable(LCD_CS , 1);
	nx_gpio_set_output_enable(LCD_RXD, 1);

    mdelay(200); // Delay 1ms
}
void LCD_SPI_Init()
{
    LCD_RST_SET;
    LCD_SCL_SET;
    LCD_SDA_CLR;
    LCD_CS_SET;

    mdelay(50); // Delay 1ms
}

/*============================================================================*/
void LCD_Init() 
{
    //DCDC_EN L->H
    LCD_PWR_SET;
    mdelay(30);

    LCD_SPI_Init();
 
    //// Reset LCD Driver////
    LCD_RST_SET;
    mdelay(30);
    LCD_RST_CLR;
    mdelay(150); // Delay 10ms // This Delay time is necessary
    LCD_RST_SET;
    mdelay(50); // Delay 50 ms

    //SSD2828_Initial
    W_C(0xb7);
    W_D(0x50);//50=TX_CLK 70=PCLK
    W_D(0x00);   //Configuration Register

    W_C(0xb8);
    W_D(0x00);
    W_D(0x00);   //VC(Virtual ChannelID) Control Register

    W_C(0xb9);
    W_D(0x00);//1=PLL disable
    W_D(0x00);
    //TX_CLK/MS should be between 5Mhz to100Mhz
    W_C(0xBA);//PLL=(TX_CLK/MS)*NS 8228=480M 4428=240M  061E=120M 4214=240M 821E=360M 8219=300M
    W_D(0x14);//D7-0=NS(0x01 : NS=1)
    W_D(0x42);//D15-14=PLL范围 00=62.5-125 01=126-250 10=251-500 11=501-1000  DB12-8=MS(01:MS=1)

    W_C(0xBB);//LP Clock Divider LP clock = 400MHz / LPD / 8 = 240 / 8 / 4 = 7.5MHz
    W_D(0x03);//D5-0=LPD=0x1 – Divide by 2
    W_D(0x00);

    W_C(0xb9);
    W_D(0x01);//1=PLL disable
    W_D(0x00);
    //MIPI lane configuration
    W_C(0xDE);//通道数
    W_D(0x00);//11=4LANE 10=3LANE 01=2LANE 00=1LANE
    W_D(0x00);

    W_C(0xc9);
    W_D(0x02);
    W_D(0x23);   //p1: HS-Data-zero  p2: HS-Data- prepare  --> 8031 issue
    delay_ms(100);
    //////////////////////////////////////////////////////////////////////
    W_C(0xB7);
    W_D(0x50);//10=TX_CLK 30=PCLK
    W_D(0x02);

    W_C(0xBD);
    W_D(0x00);
    W_D(0x00);
    ///////////////////////////////////////////////////////////////////////



    //////////////////Initial  CODE///////////////////////
    GP_COMMAD_PA(1);
    W_D(0x01); 
    //W_D(0x00);      
    delay_ms(20);
    W_C(0xB7);
    W_D(0x10);//10=TX_CLK 30=PCLK
    W_D(0x00);

    W_C(0xBD);
    W_D(0x00);
    W_D(0x00);	    
    GP_COMMAD_PA(3);
    W_D(0xb0); 
    W_D(0x00); 
    W_D(0x04); 
    delay_ms(5);

    GP_COMMAD_PA(2);
    W_D(0xb1); 
    W_D(0x00);
    delay_ms(5);

    W_C(0xB7);
    W_D(0x10);//10=TX_CLK 30=PCLK
    W_D(0x04);

    W_C(0xBD);
    W_D(0x00);
    W_D(0x00);	


    GP_COMMAD_PA(7);
    W_D(0xb3); 
    W_D(0x04); 
    W_D(0x08); 
    W_D(0x00); 
    W_D(0x22); 
    W_D(0x00); 
    W_D(0x00); 
    delay_ms(5);


    GP_COMMAD_PA(2);
    W_D(0xb4); 
    W_D(0x0c); 
    delay_ms(40);




    GP_COMMAD_PA(3);
    W_D(0xb6); 
    W_D(0x3a);
    W_D(0xd3);
    delay_ms(40);

    GP_COMMAD_PA(2);
    W_D(0xC0); 
    W_D(0x00); 
    delay_ms(40);

    W_C(0xB7);
    W_D(0x50);//10=TX_CLK 30=PCLK
    W_D(0x02);

    W_C(0xBD);
    W_D(0x00);
    W_D(0x00);


    //调节背光亮度
    GP_COMMAD_PA(2);
    W_D(0x51);              //write display brightness
    //W_D(0xE6); 
    W_D(0xD0); 
    delay_ms(40);
    GP_COMMAD_PA(2);
    W_D(0x53);              //write control display
    W_D(0x2c); 
    delay_ms(40);
    GP_COMMAD_PA(2);
    W_D(0x3a); 
    W_D(0x77); 
    delay_ms(40);

    GP_COMMAD_PA(2);
    W_D(0x35); 
    W_D(0x01);

    GP_COMMAD_PA(5);
    W_D(0x2A); 
    W_D(0x00); 
    W_D(0x00); 
    W_D(0x04); 
    W_D(0xAF); 
    delay_ms(40);
    GP_COMMAD_PA(5);
    W_D(0x2B); 
    W_D(0x00); 
    W_D(0x00); 
    W_D(0x07); 
    W_D(0x7F); 

    GP_COMMAD_PA(1);
    W_D(0x2c); 
    delay_ms(10);


    GP_COMMAD_PA(1);
    W_D(0x11); 
    delay_ms(100);

    GP_COMMAD_PA(1);
    W_D(0x29);
    delay_ms(150);
    //end init
 

    W_C(0xB7);
    W_D(0x10);//10=TX_CLK 30=PCLK
    W_D(0x04);

    W_C(0xBD);
    W_D(0x00);
    W_D(0x00);	

    GP_COMMAD_PA(7);
    W_D(0xb3); 
    W_D(0x35); 
    W_D(0x08); 
    W_D(0x00); 
    W_D(0x00); 
    W_D(0x00);
    W_D(0x00);
    delay_ms(40);





    //SSD2825_Initial
    W_C(0xb7);
    W_D(0x50);
    W_D(0x00);   //Configuration Register

    W_C(0xb8);
    W_D(0x00);
    W_D(0x00);   //VC(Virtual ChannelID) Control Register

    W_C(0xb9);
    W_D(0x00);//1=PLL disable
    W_D(0x00);

    W_C(0xBA);//PLL=(TX_CLK/MS)*NS 8228=480M 4428=240M  061E=120M 4214=240M 821E=360M 8219=300M 8225=444M 8224=432
    //W_D(0x4a);//D7-0=NS(0x01 : NS=1)       3f
    W_D(0x7d);//D7-0=NS(0x01 : NS=1)       3f
    //W_D(0xc2);//D15-14=PLL范围 00=62.5-125 01=126-250 10=251-500 11=501-1000  DB12-8=MS(01:MS=1)
    W_D(0xc3);//D15-14=PLL范围 00=62.5-125 01=126-250 10=251-500 11=501-1000  DB12-8=MS(01:MS=1)

    W_C(0xBB);//LP Clock Divider LP clock = 400MHz / LPD / 8 = 480 / 8/ 8 = 7MHz
    W_D(0x07);//D5-0=LPD=0x1 – Divide by 2
    W_D(0x00);

    W_C(0xb9);
    W_D(0x01);//1=PLL disable
    W_D(0x00);

    W_C(0xc9);
    W_D(0x02);
    W_D(0x23);   //p1: HS-Data-zero  p2: HS-Data- prepare  --> 8031 issue
    delay_ms(100);

    W_C(0xCA);
    W_D(0x01);//CLK Prepare
    W_D(0x23);//Clk Zero

    W_C(0xCB); //local_write_reg(addr=0xCB,data=0x0510)
    W_D(0x10); //Clk Post
    W_D(0x05); //Clk Per

    W_C(0xCC); //local_write_reg(addr=0xCC,data=0x100A)
    W_D(0x05); //HS Trail
    W_D(0x10); //Clk Trail

    W_C(0xd0); //local_write_reg(addr=0xCC,data=0x100A)
    W_D(0x00); //HS Trail
    W_D(0x00); //Clk Trail

    //RGB interface configuration
    W_C(0xB1);
    W_D(HSPW);//HSPW 07
    W_D(VSPW);//VSPW 05

    W_C(0xB2);
    W_D(HBPD);//HBPD 0x64=100
    W_D(VBPD);//VBPD 8 减小下移

    W_C(0xB3);
    W_D(HFPD);//HFPD 8
    W_D(VFPD);//VFPD 10

    W_C(0xB4);//Horizontal active period 720=02D0
    W_D(0xb0);//013F=319 02D0=720
    W_D(0x04);

    W_C(0xB5);//Vertical active period 1280=0500
    W_D(0x80);//01DF=479 0500=1280
    W_D(0x07);

    W_C(0xB6);//RGB CLK  16BPP=00 18BPP=01
    //W_D(0x07);//D7=0 D6=0 D5=0  D1-0=11 – 24bpp
    W_D(0x04);//D7=0 D6=0 D5=0  D1-0=00 – 16bpp
    W_D(0x20);//D15=VS D14=HS D13=CLK D12-9=NC D8=0=Video with blanking packet. 00-F0



    //MIPI lane configuration
    W_C(0xDE);//通道数
    W_D(0x03);//11=4LANE 10=3LANE 01=2LANE 00=1LANE
    W_D(0x00);

    W_C(0xD6);//  05=BGR  04=RGB
    W_D(0x05);//D0=0=RGB 1:BGR D1=1=Most significant byte sent first
    W_D(0x00);


    W_C(0xB7);
    W_D(0x29);
    W_D(0x02);

    //W_C(0x2C);
    //W_C(0x3C);


}  

unsigned short SPI_READ_DATA(unsigned char c)
{
	unsigned char  cmd,rdT;
	unsigned short reValue;
	unsigned int kk;
	
    LCD_CS_CLR;  spi_delay();

    LCD_SDA_CLR; spi_delay();		//Set DC=0, for writing to Command register
    LCD_SCL_CLR; spi_delay();
    LCD_SCL_SET; spi_delay();

	cmd = c;
	LCD_SCL_CLR;spi_delay();

	for(kk=0;kk<8;kk++)
	{
		if((cmd&0x80)==0x80) 
        {
            //LCD_SDA_SET;spi_delay();
            LCD_SDA_SET;
        }
		else
        {
            //LCD_SDA_CLR;spi_delay();
            LCD_SDA_CLR;
        }
		LCD_SCL_SET;spi_delay();
		LCD_SCL_CLR;spi_delay();
		cmd = cmd<<1;	 
	}
	
	LCD_SDA_CLR;spi_delay();		//Set DC=0, for writing to Command register
	LCD_SCL_CLR;spi_delay();
	LCD_SCL_SET;spi_delay();

	cmd = 0xFA;
	LCD_SCL_CLR;spi_delay();
	for(kk=0;kk<8;kk++)
	{
		if((cmd&0x80)==0x80)
        {
            LCD_SDA_SET;spi_delay();
        }
		else
        {
            LCD_SDA_CLR;spi_delay();
        }

		cmd = cmd<<1;	
        //printf("LCD_SDA: %d\n", LCD_SDA_GET);
		LCD_SCL_SET;spi_delay();
		LCD_SCL_CLR;spi_delay();
	}	
	
	rdT=0;
	for(kk=0;kk<8;kk++)
	{
		rdT = rdT<<1;
		LCD_SCL_SET;spi_delay();
        /*
		if((gpio->rGPBDAT)&0x0400) 
            rdT |= 0x01;
        */
		if(LCD_RXD_GET) 
            rdT |= 0x01;
		LCD_SCL_CLR;spi_delay();			
	}
	
	reValue = rdT;
	//reValue = (reValue<<8)&0xFF00;
	
	rdT=0;
	for(kk=0;kk<8;kk++)
	{
		rdT = rdT<<1;
		LCD_SCL_SET;spi_delay();
        /*
		if((gpio->rGPBDAT)&0x0400) 
            rdT |= 0x01;
        */
		if(LCD_RXD_GET) 
            rdT |= 0x01;
		LCD_SCL_CLR;spi_delay();			
	}
	
	reValue += (rdT<<8);
	
    LCD_SCL_SET;
	LCD_CS_SET;
	
	return reValue;			
}

unsigned short SPI_READ(void)
{
	unsigned char  cmd,rdT;
	unsigned short reValue;
	unsigned int kk;
	
    LCD_CS_CLR;  spi_delay();

    LCD_SDA_CLR; spi_delay();		//Set DC=0, for writing to Command register
    LCD_SCL_CLR; spi_delay();
    LCD_SCL_SET; spi_delay();

	cmd = 0xB0;
	LCD_SCL_CLR;spi_delay();

	for(kk=0;kk<8;kk++)
	{
		if((cmd&0x80)==0x80) 
        {
            //LCD_SDA_SET;spi_delay();
            LCD_SDA_SET;
        }
		else
        {
            //LCD_SDA_CLR;spi_delay();
            LCD_SDA_CLR;
        }
		LCD_SCL_SET;spi_delay();
		LCD_SCL_CLR;spi_delay();
		cmd = cmd<<1;	 
	}
	
	LCD_SDA_CLR;spi_delay();		//Set DC=0, for writing to Command register
	LCD_SCL_CLR;spi_delay();
	LCD_SCL_SET;spi_delay();

	cmd = 0xFA;
	LCD_SCL_CLR;spi_delay();
	for(kk=0;kk<8;kk++)
	{
		if((cmd&0x80)==0x80)
        {
            LCD_SDA_SET;spi_delay();
        }
		else
        {
            LCD_SDA_CLR;spi_delay();
        }

		cmd = cmd<<1;	
        //printf("LCD_SDA: %d\n", LCD_SDA_GET);
		LCD_SCL_SET;spi_delay();
		LCD_SCL_CLR;spi_delay();
	}	
	
	rdT=0;
	for(kk=0;kk<8;kk++)
	{
		rdT = rdT<<1;
		LCD_SCL_SET;spi_delay();
        /*
		if((gpio->rGPBDAT)&0x0400) 
            rdT |= 0x01;
        */
		if(LCD_RXD_GET) 
            rdT |= 0x01;
		LCD_SCL_CLR;spi_delay();			
	}
	
	reValue = rdT;
	//reValue = (reValue<<8)&0xFF00;
	
	rdT=0;
	for(kk=0;kk<8;kk++)
	{
		rdT = rdT<<1;
		LCD_SCL_SET;spi_delay();
        /*
		if((gpio->rGPBDAT)&0x0400) 
            rdT |= 0x01;
        */
		if(LCD_RXD_GET) 
            rdT |= 0x01;
		LCD_SCL_CLR;spi_delay();			
	}
	
	reValue += (rdT<<8);
	
    LCD_SCL_SET;
	LCD_CS_SET;
	
	return reValue;			
}

int SPI_READ_ID(void)
{
    int a;

	//Wr_com_data16(0xd4, 0x00FA);
    //mdelay(1);
	a=SPI_READ();
	if(a == 0x2828)
	{
        printf(" \n");
        printf("The SSD2828 ID: 0x%x  ^-^  successful !! \n",a);
        printf(" \n");
        return 0;
	}
	else
	{
        printf(" \n");
        printf("The SSD2828 ID: 0x%x  -_-! failing !!!! \n",a);
        printf(" \n");
        return 1;
	}
}

void ssd2828_set(void)
{
    printf("Begin to set brightness\n");
    delay_ms(1000*4);
    
    //调节背光亮度
    GP_COMMAD_PA(2);
    W_D(0x51);              //write display brightness
    //W_D(0xE6); 
    W_D(0xE6); 
    delay_ms(40);
    /*
    GP_COMMAD_PA(2);
    W_D(0x53);              //write control display
    W_D(0x2c); 
    delay_ms(40);
    */
    delay_ms(1000*2);
    printf("End to set brightness\n");
}

#if 1
int ssd2828_sleep(void)
{
    printf("Begin to sleep\n");

    GP_COMMAD_PA(0x01);W_D(0x28);
    delay_ms(100);
    W_C(0xF0);W_D(0x5A);W_D(0x5A);
    W_C(0xF2);W_D(0x10);W_D(0x00);W_D(0x00);W_D(0x00);W_D(0x00);W_D(0x12);W_D(0x01);W_D(0x0A);W_D(0x36);W_D(0x0E);W_D(0x0A);W_D(0x99);W_D(0x4C);
    W_C(0xF0);W_D(0xA5);W_D(0xA5);
    GP_COMMAD_PA(0x01);W_D(0x10);
    delay_ms(150);

    LCD_RST_CLR;
    delay_ms(20); // Delay 10ms // This Delay time is necessary

    LCD_POWER_OFF();
    printf("End to sleep\n");
    return 0;
}
#else
int ssd2828_sleep(void)
{
    //NORMAL MODE -> SLEEP MODE
    printf("Begin to sleep\n");
    delay_ms(20); // Delay 10ms // This Delay time is necessary

    GP_COMMAD_PA(0x01);
    W_D(0x28);
    GP_COMMAD_PA(0x01);
    W_D(0x10);

    //LCD_POWER_OFF();

    printf("End to sleep\n");
    return 0;
}
#endif

int ssd2828_spi_init(void)
{
    printf("Begin to init ssd2828\n");
    GPIO_CFG();

    //测试gpio
    int i,value;

    //配置gpio为输出功能
	nx_gpio_set_pad_function(PAD_GPIO_C,  9, 1);
	nx_gpio_set_pad_function(PAD_GPIO_C, 10, 1);
	nx_gpio_set_pad_function(PAD_GPIO_C, 11, 1);

    nx_gpio_set_pad_function(PAD_GPIO_C, 13, 1);
    nx_gpio_set_pad_function(PAD_GPIO_D,  1, 1);

    //是能gpio输出
    nx_gpio_set_output_enable(PAD_GPIO_C,  9, 1);
    nx_gpio_set_output_enable(PAD_GPIO_C, 10, 1);
    nx_gpio_set_output_enable(PAD_GPIO_C, 11, 1);

    nx_gpio_set_output_enable(PAD_GPIO_C, 13, 1);
    nx_gpio_set_output_enable(PAD_GPIO_D,  1, 1);
    for(i=0; i<20000; i++)
    {
        nx_gpio_set_output_value(PAD_GPIO_C,  9, 1);
        nx_gpio_set_output_value(PAD_GPIO_C, 10, 1);
        nx_gpio_set_output_value(PAD_GPIO_C, 11, 1);

        nx_gpio_set_output_value(PAD_GPIO_C, 13, 1);
        nx_gpio_set_output_value(PAD_GPIO_D,  1, 1);
        //printf("%8d value: gpioc9: %d gpioc10: %d\n",i, nx_gpio_get_input_value(PAD_GPIO_C, 9), nx_gpio_get_input_value(PAD_GPIO_C, 10));
        //printf("i: %3d 10: %d 11: %d\n",i, nx_gpio_get_output_value(PAD_GPIO_C, 10), nx_gpio_get_output_value(PAD_GPIO_C, 11));
        udelay(20);

        nx_gpio_set_output_value(PAD_GPIO_C,  9, 0);
        nx_gpio_set_output_value(PAD_GPIO_C, 10, 0);
        nx_gpio_set_output_value(PAD_GPIO_C, 11, 0);

        nx_gpio_set_output_value(PAD_GPIO_C, 13, 0);
        nx_gpio_set_output_value(PAD_GPIO_D,  1, 0);
        //printf("i: %3d 10: %d 11: %d\n",i, nx_gpio_get_output_value(PAD_GPIO_C, 10), nx_gpio_get_output_value(PAD_GPIO_C, 11));
        udelay(20);
    }

    //延时
    //udelay(1000*1000*1);

    //POWER OFF STATE -> SLEEP MODE
    LCD_POWER_ON();

    //SLEEP MODE -> NORMAL MODE
    LCD_Init();
    SPI_READ_ID();                //读取2828ID

    printf("The B1 addr read value: 0x%04x (set value: 0x020c)\n",SPI_READ_DATA(0xB1));
    printf("The B2 addr read value: 0x%04x (set value: 0x08be)\n",SPI_READ_DATA(0xB2));
    printf("The B3 addr read value: 0x%04x (set value: 0x0832)\n",SPI_READ_DATA(0xB3));
    printf("The B4 addr read value: 0x%04x (set value: 0x04b0)\n",SPI_READ_DATA(0xB4));
    printf("The B5 addr read value: 0x%04x (set value: 0x0780)\n",SPI_READ_DATA(0xB5));

    printf("End to init ssd2828\n");

	return 0;
}

